# -*- coding: utf-8 -*-

from Validation_Contraintes import *
from RequeteSQL import *
import xmlrpclib
#import pickle
import tkMessageBox as msgBox
try:
    from xTkinter import *
except:
    print >> sys.stderr, 'Le module xTkinter est  introuvable'  
    sys.exit(1)
import sys
Validateur = ValidationContrainte()
RequeteSQL = RequeteSQL()
NomRegle = 'OPERANDE_GAUCHE ||' + "' '" + '|| ID_CONTRAINTES '
class Vue_ReglesAffaires(Frame):
    def __init__(self, master,parent,**cnf):
        msgBox.showerror('attentionM','executer le fichier sql code_sql_a_executer, et tester cette vue avec la table s_emp \n')
        KeysList=cnf.keys()
        self.parent = parent
        self.master=master
        self.width=400
        self.height=200
        if ('width' in KeysList):
            self.width=cnf['width']
        if ('width' in KeysList):
            self.height=cnf['height']
        Frame.__init__(self,master,cnf)        
        self.config(bg='Floralwhite')#(bg='#6B979C',relief='raised',bd=3)
        self.pack(side=LEFT, anchor=NW)
        self.configure(width=850,height=500)
        
        #Aller chercher les règles d'affaires existantes
        Requete = RequeteSQL.sqlWhere('PERP_CONTRAINTES', [NomRegle])
        Reponse = self.parent.serveur.ExecuterRequeteSQLWhere(Requete)
        self.ListeReglesAffaires = Reponse[1]
        
        # CREATION DU GUI RÈGLES D'AFFAIRES   
                
        self.Regles_Affaires_Label=xLabel(self,text='Règles d''affaires',height=1,width=14)
        self.Regles_Affaires_Label.place(x=10,y=40)
        self.Regles_Affaires=xCombobox(self,width=30)
        for Regle in self.ListeReglesAffaires:
            self.Regles_Affaires.addItem(Regle[0])
        self.Regles_Affaires.setListWidth(25)
        self.Regles_Affaires.setListHeight(5)
        self.Regles_Affaires.setText('<Choisir>')
        self.Regles_Affaires.place(x=100,y=40)
        self.Regles_Affaires.setValidator(self.obtenirContraintes)
        
        
        self.Creer_Button=xButton(self,text='Créer',height=1,width=10)
        self.Creer_Button.place(x=325,y=40)
        
        self.Sauvegarder_Button=xButton(self,text='Sauvegarder',height=1,width=10)
        self.Sauvegarder_Button.place(x=325,y=80)
        
        self.Supprimer_Button=xButton(self,text='Supprimer',height=1,width=10)
        self.Supprimer_Button.place(x=450,y=80)
              
        self.Operande_Droite_Label=xLabel(self,text='Opérande',height=1,width=20)
        self.Operande_Droite_Label.place(x=520,y=125)
        self.Operande_Gauche_Label=xLabel(self,text='Opérande',height=1,width=20)
        self.Operande_Gauche_Label.place(x=175,y=125)

        self.Parent_Combo_Box=xCombobox(self,width=8)
        self.Parent_Combo_Box.place(x=10,y=175)
        self.Parent_Label=xLabel(self,text='Parent',height=1,width=8)
        self.Parent_Label.place(x=10,y=150)
       
        self.Condition_Combo_Box=xCombobox(self,width=8)
        self.Condition_Combo_Box.place(x=90,y=175)
        self.Condition_Label=xLabel(self,text='Condition',height=1,width=8)
        self.Condition_Label.place(x=90,y=150)
                
        self.Table_Operande_Gauche=xCombobox(self,width=20)
        self.Table_Operande_Gauche.place(x=165,y=175)
        self.Table1_Label=xLabel(self,text='Opérande',height=1,width=20)
        self.Table1_Label.place(x=165,y=150)
        
        self.Attribut1_Label=xLabel(self,text='Attribut',height=1,width=20)
        self.Attribut1_Label.place(x=310,y=150)
        self.Attribut_Operande_Gauche=xCombobox(self,width=20)
        self.Attribut_Operande_Gauche.place(x=310,y=175)
        
        self.Comparaison_Label=xLabel(self,text='Comparaison',height=1,width=10)
        self.Comparaison_Label.place(x=460,y=150)
        self.Comparaison_Combo_Box=xCombobox(self)
        self.Comparaison_Combo_Box.width=10
        for Operande in Validateur.getOperande():
            self.Comparaison_Combo_Box.addItem(Operande)
        self.Comparaison_Combo_Box.setListWidth(25)
        self.Comparaison_Combo_Box.setListHeight(5)
        self.Comparaison_Combo_Box.setText('<Choisir>')
        self.Comparaison_Combo_Box.place(x=460,y=175)
        
        self.Table_Operande_Droite=xCombobox(self,width=20)
        self.Table_Operande_Droite.place(x=550,y=175)
        self.Table2_Label=xLabel(self,text='Opérande',height=1,width=20)
        self.Table2_Label.place(x=550,y=150)
        
        self.Attribut2_Label=xLabel(self,text='Attribut',height=1,width=20)
        self.Attribut2_Label.place(x=695,y=150)
        self.Attribut_Operande_Droite=xCombobox(self,width=20)
        self.Attribut_Operande_Droite.place(x=695,y=175)
        
        self.Message_Erreur_Text=xText(self,text='Message d''erreur',height=1,width=50)
        self.Message_Erreur_Text.place(x=295,y=365)
        self.Message_Erreur_Label=xLabel(self,text='Message d''erreur',height=1,width=15)
        self.Message_Erreur_Label.place(x=165,y=365)
                
        self.Conseils_Label=xLabel(self,text='Conseils',height=1,width=10)
        self.Conseils_Label.place(x=225,y=405)
        self.Conseils_Text=xText(self,text='Conseils',height=1,width=50)
        self.Conseils_Text.place(x=295,y=405)
        
        self.Annuler_Button=xButton(self,text='Ajouter',height=1,width=10)
        self.Annuler_Button.place(x=750,y=320)
                
        self.Annuler_Button=xButton(self,text='Annuler',height=1,width=10)
        self.Annuler_Button.place(x=750,y=360)
        
        self.Quitter_Button=xButton(self,text='Quitter',height=1,width=10)
        self.Quitter_Button.place(x=750,y=400)
        
        self.AssociationColonneChamps = {
                                        "OPERANDE_GAUCHE":self.Attribut_Operande_Gauche,
                                        "CONDITION":self.Condition_Combo_Box,
                                        "COMPARAISON":self.Comparaison_Combo_Box,
                                        "OPERANDE_DROITE":self.Table_Operande_Droite,
                                        "ATTRIBUT_DROITE":self.Attribut_Operande_Droite,
                                        "MESSAGE_ERREUR":self.Message_Erreur_Text,
                                        "MESSAGE_GUIDE":self.Conseils_Text
                                        }
        pass

    def Quitter(self):
        #if MsgBox.askokcancel("Attention", "Vous annulez l'ouverture de session."):
        self.destroy()
        pass
    
    def close(self):
        self.destroy()
        pass

    def obtenirContraintes(self):
        print "aller chercher contraintes de ", self.Regles_Affaires.getText()
        ListeContraintes =  self.AssociationColonneChamps.keys()
        Requete = RequeteSQL.sqlWhere('PERP_CONTRAINTES',ListeContraintes, [NomRegle + "=" + "'" + self.Regles_Affaires.getText()+ "'"] )
        Reponse = self.parent.serveur.ExecuterRequeteSQLWhere(Requete)
        for index in range (len(Reponse[1][0])):
            self.AssociationColonneChamps[ListeContraintes[index]].setText(Reponse[1][0][index])
        pass
        
class Controleur:
    def __init__(self):
        Serveur='http://localhost:8000'
        self.serveur=xmlrpclib.ServerProxy(Serveur)
        root=Tk()
        root.geometry('1000x600')
        vi=Vue_ReglesAffaires(root,self)
        root.mainloop()       

#Controleur = Controleur()
     
